<template>
  <view class="payment-duration-stats-container">
    <z-paging 
      ref="paging" 
      v-model="dataList" 
      @query="queryList"
      :auto="false"
      :refresher-enabled="true"
      :loading-more-enabled="true"
      :empty-view-text="'暂无数据'"
      class="list-paging"
    >
      <template #top>
        <!-- 页面标题 -->
        <view class="page-header">
          <view class="header-title">缴费时长统计</view>
        </view>
        
        <!-- 筛选区域 -->
        <view class="filter-section">
          <view class="filter-row">
            <view class="filter-item">
              <text class="filter-label">时间范围：</text>
              <picker mode="selector" :value="filters.timeRangeIndex" :range="timeRangeOptions" @change="onTimeRangeChange">
                <view class="date-picker">
                  <text>{{ filters.timeRangeIndex >= 0 ? timeRangeOptions[filters.timeRangeIndex] : '请选择' }}</text>
                  <up-icon name="arrow-down" size="14"></up-icon>
                </view>
              </picker>
            </view>
            
            <view class="filter-item">
              <text class="filter-label">统计周期：</text>
              <picker mode="selector" :value="filters.periodIndex" :range="periodOptions" @change="onPeriodChange">
                <view class="date-picker">
                  <text>{{ filters.periodIndex >= 0 ? periodOptions[filters.periodIndex] : '请选择' }}</text>
                  <up-icon name="arrow-down" size="14"></up-icon>
                </view>
              </picker>
            </view>
          </view>
          
          <view class="filter-row">
            <view class="filter-item">
              <text class="filter-label">停车场：</text>
              <picker :value="filters.parkingLotIndex" :range="parkingLotOptions" @change="onParkingLotChange">
                <view class="date-picker">
                  <text>{{ filters.parkingLotIndex >= 0 ? parkingLotOptions[filters.parkingLotIndex] : '全部' }}</text>
                  <up-icon name="arrow-down" size="14"></up-icon>
                </view>
              </picker>
            </view>
          </view>
          
          <view class="filter-actions">
            <button class="search-btn" @click="searchData">查询</button>
            <button class="reset-btn" @click="resetFilters">重置</button>
          </view>
        </view>
        
        <!-- 列表表头 -->
        <view class="list-header">
          <text class="list-cell">时长区间</text>
          <text class="list-cell">订单数量</text>
          <text class="list-cell">占比</text>
          <text class="list-cell">金额</text>
          <text class="list-cell">平均单价</text>
        </view>
      </template>
      
      <!-- 列表内容 -->
      <view v-for="(item, index) in dataList" :key="index" class="list-row" @click="showDetail(item)">
        <text class="list-cell">{{ item.durationRange }}</text>
        <text class="list-cell">{{ item.orderCount }}</text>
        <text class="list-cell">{{ item.percentage }}%</text>
        <text class="list-cell">{{ item.totalAmount }}元</text>
        <text class="list-cell">{{ item.averagePrice }}元</text>
      </view>
    </z-paging>
    
    <!-- 详情弹窗 -->
    <view class="detail-modal" v-if="showDetailModal">
      <view class="detail-content">
        <view class="detail-header">
          <text class="detail-title">详情信息</text>
          <text class="close-btn" @click="showDetailModal = false">×</text>
        </view>
        
        <view class="detail-body" v-if="currentDetail">
          <view class="detail-item">
            <text class="detail-label">时长区间：</text>
            <text class="detail-value">{{ currentDetail.durationRange }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">统计周期：</text>
            <text class="detail-value">{{ periodOptions[filters.periodIndex] }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">停车场：</text>
            <text class="detail-value">{{ currentDetail.parkingLot }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">订单数量：</text>
            <text class="detail-value">{{ currentDetail.orderCount }}</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">占比：</text>
            <text class="detail-value">{{ currentDetail.percentage }}%</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">金额：</text>
            <text class="detail-value">{{ currentDetail.totalAmount }}元</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">平均单价：</text>
            <text class="detail-value">{{ currentDetail.averagePrice }}元</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">最高单价：</text>
            <text class="detail-value">{{ currentDetail.maxPrice }}元</text>
          </view>
          
          <view class="detail-item">
            <text class="detail-label">最低单价：</text>
            <text class="detail-value">{{ currentDetail.minPrice }}元</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'

// 筛选条件
const filters = reactive({
  timeRangeIndex: 0,
  periodIndex: 0,
  parkingLotIndex: -1
})

// 时长区间选项
const timeRangeOptions = [
  '全部',
  '一天内',
  '一周内',
  '一个月内',
  '45天内',
  '超过一个月'
]

// 统计周期选项
const periodOptions = [
  '日统计',
  '月统计',
  '年统计'
]

// 停车场选项
const parkingLotOptions = [
  '全部',
  'A区停车场',
  'B区停车场',
  'C区停车场',
  'D区停车场'
]

// 列表数据
const dataList = ref([])
const paging = ref(null)

// 详情弹窗
const showDetailModal = ref(false)
const currentDetail = ref(null)

// 时长区间选择处理
const onTimeRangeChange = (e) => {
  filters.timeRangeIndex = parseInt(e.detail.value)
}

// 统计周期选择处理
const onPeriodChange = (e) => {
  filters.periodIndex = parseInt(e.detail.value)
}

// 停车场选择处理
const onParkingLotChange = (e) => {
  filters.parkingLotIndex = parseInt(e.detail.value)
}

// 查询数据
const searchData = () => {
  if (paging.value) {
    paging.value.reload()
  }
}

// 重置筛选条件
const resetFilters = () => {
  filters.timeRangeIndex = 0
  filters.periodIndex = 0
  filters.parkingLotIndex = -1
  
  nextTick(() => {
    if (paging.value) {
      paging.value.reload()
    }
  })
}

// 查询列表数据
const queryList = (pageNo, pageSize) => {
  // 模拟请求延迟
  setTimeout(() => {
    // 生成模拟数据
    const data = generateMockData(pageNo, pageSize)
    paging.value.complete(data)
  }, 500)
}

// 生成模拟数据
const generateMockData = (pageNo, pageSize) => {
  const data = []
  
  // 定义时长区间
  const durationRanges = [
    '0-1小时',
    '1-3小时',
    '3-6小时',
    '6-12小时',
    '12-24小时',
    '1-3天',
    '3-7天',
    '7-30天',
    '30-45天',
    '45天以上'
  ]
  
  // 根据筛选条件过滤时长区间
  let filteredRanges = [...durationRanges]
  if (filters.timeRangeIndex > 0) {
    switch (filters.timeRangeIndex) {
      case 1: // 一天内
        filteredRanges = durationRanges.slice(0, 5)
        break
      case 2: // 一周内
        filteredRanges = durationRanges.slice(0, 7)
        break
      case 3: // 一个月内
        filteredRanges = durationRanges.slice(0, 8)
        break
      case 4: // 45天内
        filteredRanges = durationRanges.slice(0, 9)
        break
      case 5: // 超过一个月
        filteredRanges = durationRanges.slice(8)
        break
    }
  }
  
  // 停车场列表
  const parkingLots = parkingLotOptions.slice(1) // 排除"全部"选项
  
  // 计算总订单数（用于计算占比）
  const totalOrders = Math.floor(Math.random() * 1000) + 2000 // 2000-3000
  let remainingOrders = totalOrders
  
  // 生成随机数据
  for (let i = 0; i < filteredRanges.length; i++) {
    // 最后一个区间使用剩余订单数
    let orderCount
    if (i === filteredRanges.length - 1) {
      orderCount = remainingOrders
    } else {
      // 根据时长区间设置不同的订单数量分布
      let percentage
      if (i < 3) {
        // 短时间停车（更常见）
        percentage = Math.random() * 0.15 + 0.1 // 10%-25%
      } else if (i < 6) {
        // 中等时间停车
        percentage = Math.random() * 0.1 + 0.05 // 5%-15%
      } else {
        // 长时间停车（较少）
        percentage = Math.random() * 0.05 + 0.01 // 1%-6%
      }
      orderCount = Math.floor(totalOrders * percentage)
      remainingOrders -= orderCount
    }
    
    // 计算占比
    const percentage = ((orderCount / totalOrders) * 100).toFixed(2)
    
    // 生成金额（根据时长区间设置不同的价格范围）
    let basePrice
    if (i < 3) {
      basePrice = 5 + i * 3 // 5-11元基础价
    } else if (i < 6) {
      basePrice = 15 + (i - 3) * 10 // 15-35元基础价
    } else {
      basePrice = 50 + (i - 6) * 30 // 50-140元基础价
    }
    
    // 添加一些随机波动
    const priceVariation = basePrice * (Math.random() * 0.4 - 0.2) // -20%到+20%的波动
    const averagePrice = (basePrice + priceVariation).toFixed(2)
    const totalAmount = (orderCount * averagePrice).toFixed(2)
    
    // 最高和最低单价
    const maxPrice = (averagePrice * (1 + Math.random() * 0.5)).toFixed(2)
    const minPrice = (averagePrice * (1 - Math.random() * 0.4)).toFixed(2)
    
    // 随机选择停车场
    const selectedParkingLotIndex = filters.parkingLotIndex > 0 
      ? filters.parkingLotIndex - 1 
      : Math.floor(Math.random() * parkingLots.length)
    const parkingLot = parkingLots[selectedParkingLotIndex]
    
    data.push({
      id: i + 1,
      durationRange: filteredRanges[i],
      orderCount,
      percentage,
      totalAmount,
      averagePrice,
      maxPrice,
      minPrice,
      parkingLot
    })
  }
  
  return data
}

// 显示详情
const showDetail = (item) => {
  currentDetail.value = item
  showDetailModal.value = true
}

// 页面加载
onMounted(() => {
  // 页面挂载时获取数据
  nextTick(() => {
    if (paging.value) {
      paging.value.reload()
    }
  })
})
</script>

<style lang="scss" scoped>
.payment-duration-stats-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

.list-paging {
  flex: 1;
  height: 100%;
}

.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40rpx 30rpx 30rpx;
  color: white;
  
  .header-title {
    font-size: 36rpx;
    font-weight: bold;
    text-align: center;
  }
}

.filter-section {
  background: white;
  margin: 20rpx;
  padding: 30rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  
  .filter-row {
    display: flex;
    margin-bottom: 20rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .filter-item {
    flex: 1;
    margin-right: 20rpx;
    
    &:last-child {
      margin-right: 0;
    }
    
    &.full-width {
      flex: none;
      width: 100%;
      margin-right: 0;
    }
  }
  
  .filter-label {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 10rpx;
    display: block;
  }
  
  .date-picker,
  .picker-input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 10rpx;
    background: #fafafa;
    font-size: 26rpx;
    color: #666;
  }
  
  .filter-actions {
    display: flex;
    gap: 20rpx;
    margin-top: 30rpx;
  }
  
  .search-btn,
  .reset-btn {
    flex: 1;
    padding: 20rpx;
    border-radius: 10rpx;
    font-size: 28rpx;
    border: none;
  }
  
  .search-btn {
    background: #1890FF;
    color: white;
  }
  
  .reset-btn {
    background: #f0f0f0;
    color: #666;
  }
}

.list-header {
  display: flex;
  background: #f0f0f0;
  padding: 20rpx;
  margin: 0 20rpx;
  border-radius: 10rpx 10rpx 0 0;
  font-size: 26rpx;
  font-weight: bold;
  color: #333;
}

.list-row {
  display: flex;
  background: white;
  padding: 20rpx;
  margin: 0 20rpx;
  border-bottom: 2rpx solid #f0f0f0;
  font-size: 26rpx;
  color: #666;
  
  &:last-child {
    border-radius: 0 0 10rpx 10rpx;
    margin-bottom: 20rpx;
  }
}

.list-cell {
  flex: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}

.detail-content {
  width: 80%;
  max-height: 80%;
  background: white;
  border-radius: 20rpx;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  background: #f5f5f5;
  border-bottom: 2rpx solid #e0e0e0;
  
  .detail-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }
  
  .close-btn {
    font-size: 40rpx;
    color: #999;
    line-height: 1;
  }
}

.detail-body {
  padding: 30rpx;
  overflow-y: auto;
}

.detail-item {
  display: flex;
  margin-bottom: 20rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .detail-label {
    width: 200rpx;
    font-size: 28rpx;
    color: #666;
  }
  
  .detail-value {
    flex: 1;
    font-size: 28rpx;
    color: #333;
  }
}
</style>